package com.dayaokeji.rhythmschoolstudent.client.common.exam.subject_type;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.dayaokeji.rhythmschoolstudent.R;
import com.dayaokeji.rhythmschoolstudent.client.common.exam.adapter.SubjectImgAdapter;
import com.dayaokeji.rhythmschoolstudent.client.common.exam.subject_type.utils.ScoreUtil;
import com.dayaokeji.rhythmschoolstudent.utils.ObjectUtils;
import com.dayaokeji.rhythmschoolstudent.utils.ToastUtils;
import com.dayaokeji.server_api.domain.ExamQuestion;
import com.dayaokeji.server_api.domain.SubjectAnswer;

import butterknife.BindView;

/**
 * 填空题
 */
public class BlankSubjectFragment extends BaseSubjectFragment {


    @BindView(R.id.tv_subject_type)
    TextView tvSubjectType;
    @BindView(R.id.tv_subject_score)
    TextView tvSubjectScore;
    @BindView(R.id.tv_subject_content)
    TextView tvSubjectContent;
    @BindView(R.id.rv_subject_imgs)
    RecyclerView rvSubjectImages;
    @BindView(R.id.et_blank_answer)
    EditText etBlankAnswer;
    @BindView(R.id.iv_subject_state)
    ImageView ivSubjectState;

    private ExamQuestion examQuestion;
    private final SubjectImgAdapter subjectImgAdapter = new SubjectImgAdapter();
    private SubjectAnswer subjectAnswer = new SubjectAnswer();

    public static BlankSubjectFragment newInstance(ExamQuestion examQuestion) {
        Bundle args = new Bundle();
        args.putSerializable(EXTRA_EXAM_ENTITY,examQuestion);
        BlankSubjectFragment fragment = new BlankSubjectFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    protected View onCreate(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_blank_subject, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        init();
    }

    private void init() {
        examQuestion = (ExamQuestion) getArguments().getSerializable(EXTRA_EXAM_ENTITY);
        if (examQuestion == null) {
            ToastUtils.warning(R.string.data_incorrect);
            return;
        }
        subjectAnswer.setExamQuestionId(examQuestion.getId());

        if (examQuestion.getSubjectAnswer() != null) {
            examQuestion.setStudentAnswer(examQuestion.getSubjectAnswer().getAnswer());
        }

        if (!TextUtils.isEmpty(examQuestion.getStudentAnswer())) {
            subjectAnswer.setAnswer(examQuestion.getStudentAnswer());
            callbackResult(subjectAnswer);
        }
        /*if (getUserSubjectAnswer() != null) {
            subjectAnswer = getUserSubjectAnswer();
        }*/
        setupView();
        setupViewData();
        setOnListnener();
    }

    private void setupView() {
        rvSubjectImages.setLayoutManager(new GridLayoutManager(getActivity(),4));
        rvSubjectImages.setAdapter(subjectImgAdapter);
        setNavigationShowImage(subjectImgAdapter);
    }

    private void setupViewData() {
        tvSubjectType.setText(getSubjectTypeStr(examQuestion.getType()));
        tvSubjectScore.setText(String.format("分值：%s 分", ScoreUtil.doubleTrans(examQuestion.getScore())));
        tvSubjectContent.setText(examQuestion.getContent());
        subjectImgAdapter.setNewData(examQuestion.getResourceList());

        if (isExamResult()) {
            etBlankAnswer.setEnabled(false);
            if (examQuestion.getGetScore() > 0) {
                ivSubjectState.setImageResource(R.mipmap.ic_subject_success);
            }else{
                ivSubjectState.setImageResource(R.mipmap.ic_subject_error);
            }
        }
        if (!TextUtils.isEmpty(examQuestion.getStudentAnswer())) {
            etBlankAnswer.setText(examQuestion.getStudentAnswer());
        }else{
            etBlankAnswer.setHint("请输入答案");
        }

        if (isShowResult()) {
            String correctAnswer = examQuestion.getCorrectAnswer();
            if (TextUtils.isEmpty(correctAnswer)) {
                etBlankAnswer.setHint("");
                etBlankAnswer.setText(correctAnswer);
            }else{
                etBlankAnswer.setText(correctAnswer);
            }

            ivSubjectState.setVisibility(View.GONE);
        }else{
            if (isExamResult()) {
                ivSubjectState.setVisibility(View.VISIBLE);
            }
        }

    }

    private void setOnListnener() {
        etBlankAnswer.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                subjectAnswer.setAnswer(s.toString());
                callbackResult(subjectAnswer);
            }
        });
    }
}
